<template>
    <div id="box">
      <el-card class="!border-none" shadow="never">
          <el-row>
              <el-col :span="20">
                <el-button type="primary" @click="check(2)">审核通过</el-button>
                    <el-button type="primary" @click="check(3)" style="margin-left: 20px;">审核驳回</el-button>
                <el-badge :value="bsnum<=0?'':bsnum" class="item">
                  <el-button type="primary" style="margin-left: 15px;">
                    <router-link :to="{path: getRoutePath('stock.check/sexamine')}">
                      数据补审
                    </router-link>
                  </el-button>
                </el-badge>
                <el-button type="primary" @click="exportExcel" style="margin-left: 15px;">报表导出</el-button>
              </el-col>
          </el-row>
      </el-card>
      <el-card id="scroll-box" class="!border-none mt-4" shadow="never">
        <div class="bltitle">
          {{newdate}}各分厂药剂库存统计表
        </div>
        <kucun id="stockComponent" ref="stockComponent" :date="newdate" :isup="true" />
      </el-card>
    </div>
</template>
<script lang="ts" setup name="stockSubmitSupplement">
import { getRoutePath } from '@/router'
import kucun from '@/components/stock/kucun.vue';

import cache from "@/utils/cache";
import { checkStock, getStockNocheck, exportExcelStockApi } from "@/api/check";
import { GetDateStr, timeFormat } from '@/utils/util'
const date = ref(GetDateStr(-1))
const newdate = computed(() => {
  let d = new Date().getHours();
  if(d<17){
    return date.value
  }else{
    return timeFormat()
  }
})

const bsnum = ref(0)
onMounted(() => {
  getStockNocheck().then((res)=>{
    console.log('=====',res)
    if(res.count>0){
      bsnum.value = res.count
    }
  })
})

// 提交方法，调用接口
const check = async (status:any) => {
  ElMessageBox.confirm(
    '您确定要审核吗？',
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
  .then(() => {
    let userInfo = cache.get('userInfo')
    checkStock({
      cid: userInfo.id,
      cname: userInfo.name,
      date:newdate.value,
      status
    }).then(()=>{
      bsnum.value = bsnum.value-1
    })
  })
  
}

function exportExcel(){
  exportExcelStockApi({
    date:newdate.value
  }).then((res:any) => {
    console.log(res)
    window.location.href=res.path
  })
  // let content = document.querySelector('#stockComponent').innerHTML
  // const uri = 'data:text/excel;charset=utf-8,\ufeff' + encodeURIComponent(content);
  // const link = document.createElement("a");
  // link.href = uri;
  // // 下载的文件命名 也可以（5月数据表.xlsx）
  // link.download =  date.value+"各分厂药剂库存统计表.xls";
  // link.click();
  
}
</script>

<style>
  .el-form .el-form-item{
    margin-bottom: 0;
  }
  .bltitle{
    height: 44px;
    font-size: 20px;
    text-align: center;
    font-weight: bold;
  }
  .location{
    position: fixed;
    top:50%;
    left:230px;
  }
  .list{
    position: fixed;
    text-align: center;
    width: 200px;
    background: #eee;
    top:15%;
    left:230px;
    border-radius: 5px;
    border: 1px solid #aaa;
  }
  .item{
    margin:10px 5px;
  }
</style>